package com.zhelun.demo.ada;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhelun.demo.R;
import com.zhelun.demo.SubjectsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhelun on 2018/1/18.
 */

public class TopMovieAda extends BaseAdapter {
    private List<SubjectsBean> list = new ArrayList<>();
    private Context context;

    public TopMovieAda(List<SubjectsBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_movie, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        Glide.with(context).load(list.get(i).getImages().getSmall()).into(holder.ivImg);
        holder.tvName.setText(list.get(i).getTitle());
        holder.tvDirector.setText(list.get(i).getDirectors().get(0).getName());
        StringBuilder actor = new StringBuilder();
        for (int m = 0; m < list.get(i).getCasts().size(); m++) {
            actor.append(list.get(i).getCasts().get(m).getName());
            actor.append("/");
        }
        if (actor.length() != 0) {
            holder.tvActor.setText(actor.toString().substring(0, actor.length() - 1));
        } else {
            holder.tvActor.setText(actor.toString());
        }


        StringBuilder type = new StringBuilder();
        for (int k = 0; k < list.get(i).getGenres().size(); k++) {
            type.append(list.get(i).getGenres().get(k));
            type.append("/");
        }
        if (type.length() != 0) {
            holder.tvType.setText(type.toString().substring(0, type.length() - 1));
        } else {
            holder.tvType.setText(type.toString());
        }

        holder.tvPoint.setText(list.get(i).getRating().getAverage() + "");
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.iv_img)
        ImageView ivImg;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_director)
        TextView tvDirector;
        @BindView(R.id.tv_actor)
        TextView tvActor;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_point)
        TextView tvPoint;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
